<?php defined('InShopNC') or exit('Access Invalid!');?>
<div class="tabmenu">
  <?php include template('layout/submenu');?>
</div>
<div class="ncsc-form-default">
	<form id="add_form" action="index.php?act=lottery_activity&op=activity_save" method="post" enctype="multipart/form-data">
		<dl>
			<dt>
				<i class="required">*</i>活动编号：
			</dt>
			<dd>
				<input class="w400 text" name="activity_id" type="text" id="activity_id" value="" /> <span></span>
			</dd>
		</dl>
		<dl>
			<dt>
				<i class="required">*</i>活动名称：
			</dt>
			<dd>
				<input class="w400 text" name="activity_name" type="text" id="activity_name" value="" maxlength="30" /> <span></span>
				<p class="hint">活动名称最多可输入30个字符</p>
			</dd>
		</dl>
		<dl>
			<dt>兑奖开始时间：</dt>
			<dd>
				<input id="exchange_start_date" name="exchange_start_date" type="text" class="text w130" /><em class="add-on"><i class="icon-calendar"></i></em><span></span>
			</dd>
		</dl>
		<dl>
			<dt>兑奖结束时间：</dt>
			<dd>
				<input id="exchange_end_date" name="exchange_end_date" type="text" class="text w130" /><em class="add-on"><i class="icon-calendar"></i></em><span></span>
			</dd>
		</dl>
		<dl>
			<dt>
				<i class="required">*</i>关联礼品
			</dt>
			<dd>
				<input id="goods_ids" name="goods_ids" type="hidden" />
				<input id="short_names" name="short_names" type="hidden" />
				<input id="pro_nums" name="pro_nums" type="hidden" />
				<input id="gift_ids" name="gift_ids" type="hidden" />
				
				<ul class="ncsc-form-radio-list">
                <li>
                  <input id="goods_type" name="goods_type" class="radio" type="radio" checked="checked" value="1">
                  <label for="goods_type">商品</label>
                  <div nctype="div_freight">
                      <input id="groupbuy_goods_id" name="groupbuy_goods_id" type="hidden" value=""/>
                      <span></span>
                      <div id="div_search_goods" class="div-goods-select mt10">
                          <table class="search-form">
                              <tr>
                                  <th class="w150">
                                      <strong>第一步：搜索店内商品</strong>
                                  </th>
                                  <td class="w160">
                                      <input id="search_goods_name" type="text w150" class="text" name="goods_name" value=""/>
                                  </td>
                                  <td class="w70 tc">
                                      <a href="javascript:void(0);" id="btn_search_goods" class="ncsc-btn"/><i class="icon-search"></i><?php echo $lang['nc_search'];?></a></td>
                                    <td class="w10"></td>
                                    <td>
                                        <!-- <p class="hint">不输入名称直接搜索将显示店内所有普通商品，特殊商品不能参加。</p> -->
                                    </td>
                                </tr>
                            </table>
                            <div id="div_goods_search_result" class="search-result" style="width:739px;"></div>
                            <!-- <a id="btn_hide_search_goods" class="close" href="javascript:void(0);">X</a> -->
                        </div>
                  </div>
                </li>
                <li>
                  <input id="goods_type" name="goods_type" class="radio" type="radio" value="2">
                  <label for="goods_type">优惠券</label>
                  <div nctype="div_freight">
                      <input id="groupbuy_goods_id" name="groupbuy_goods_id" type="hidden" value=""/>
                      <span></span>
                      <div id="div_search_voucher" class="div-goods-select mt10" style="display: none;">
                          <table class="search-form">
                              <tr>
                                  <th class="w150">
                                      <strong>第一步：搜索店内优惠券</strong>
                                  </th>
                                  <td class="w160">
                                      <input id="voucher_title" type="text w150" class="text" name="voucher_title" value=""/>
                                  </td>
                                  <td class="w70 tc">
                                      <a href="javascript:void(0);" id="btn_search_voucher" class="ncsc-btn"/><i class="icon-search"></i><?php echo $lang['nc_search'];?></a></td>
                                    <td class="w10"></td>
                                    <td>
                                        <p class="hint"></p>
                                    </td>
                                </tr>
                            </table>
                            <div id="div_voucher_search_result" class="search-result" style="width:739px;"></div>
                            <!-- <a id="btn_hide_search_voucher" class="close" href="javascript:void(0);">X</a> -->
                        </div>
                  </div>
                </li>
                </ul>
			</dd>
		</dl>
    	<table class="ncsc-default-table">
              <thead>
                <tr>
                  <th class="w10"></th>
                  <th class="w50"></th>
                  <th class="tl">商品名称</th>
                  <th class="w50">礼品编号</th>
                  <th class="w130">商品简称</th>
                  <th class="w130">商品数量</th>
                  <th class="w100"><?php echo $lang['nc_handle'];?></th>
                </tr>
              </thead>
              <tbody id="goods_table">
                
              </tbody>
            </table>	
		<div class="bottom">
			<label class="submit-border"> <input type="button" class="submit" value="<?php echo $lang['nc_submit'];?>"></label>
		</div>
	</form>
</div>


<link rel="stylesheet" type="text/css" href="<?php echo RESOURCE_SITE_URL;?>/js/jquery-ui/themes/ui-lightness/jquery.ui.css" />
<link rel="stylesheet" type="text/css" href="<?php echo RESOURCE_SITE_URL;?>/js/jquery-ui-timepicker-addon/jquery-ui-timepicker-addon.min.css" />
<script src="<?php echo RESOURCE_SITE_URL;?>/js/jquery.ajaxContent.pack.js"></script>
<script src="<?php echo RESOURCE_SITE_URL;?>/js/jquery-ui/i18n/zh-CN.js"></script>
<script src="<?php echo RESOURCE_SITE_URL;?>/js/jquery-ui-timepicker-addon/jquery-ui-timepicker-addon.min.js"></script>
<script type="text/javascript" src="<?php echo RESOURCE_SITE_URL;?>/js/fileupload/jquery.iframe-transport.js" charset="utf-8"></script>
<script type="text/javascript" src="<?php echo RESOURCE_SITE_URL;?>/js/fileupload/jquery.ui.widget.js" charset="utf-8"></script>
<script type="text/javascript" src="<?php echo RESOURCE_SITE_URL;?>/js/fileupload/jquery.fileupload.js" charset="utf-8"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#exchange_start_date').datetimepicker({
        controlType: 'select'
    });

    $('#exchange_end_date').datetimepicker({
        controlType: 'select'
    });

    var goods_type = $("#goods_type").val()|'';
    if(goods_type == 1) {
    	$('#div_search_goods').show();
    	$('#div_search_voucher').hide();
    } else {
    	$('#div_search_goods').hide();
    	$('#div_search_voucher').show();
    }

    $(".radio").change(function() {
    	var selectedvalue = $("input[name='goods_type']:checked").val();
        if(selectedvalue == 1) {
        	$('#div_search_goods').show();
        	$('#div_search_voucher').hide();
        } else {
        	$('#div_search_goods').hide();
        	$('#div_search_voucher').show();
        }
    });

    $('#btn_hide_search_goods').on('click', function() {
        $('#div_search_goods').hide();
    });
    
    $('#btn_hide_search_voucher').on('click', function() {
        $('#div_search_voucher').hide();
    });

    //搜索商品
    $('#btn_search_goods').on('click', function() {
        var url = "<?php echo urlShop('lottery_activity', 'search_goods');?>";
        url += '&' + $.param({goods_name: $('#search_goods_name').val()});
        $('#div_goods_search_result').load(url);
    });
    
    //搜索优惠券
    $('#btn_search_voucher').on('click', function() {
        var url = "<?php echo urlShop('lottery_activity', 'search_voucher');?>";
        url += '&' + $.param({voucher_title: $('#voucher_title').val()});
        $('#div_voucher_search_result').load(url);
    });

    $('#div_goods_search_result').on('click', 'a.demo', function() {
        $('#div_goods_search_result').load($(this).attr('href'));
        return false;
    });
    
    $('#div_voucher_search_result').on('click', 'a.demo', function() {
        $('#div_voucher_search_result').load($(this).attr('href'));
        return false;
    });

    //选择商品
    $('#div_goods_search_result').on('click', '[nctype="btn_add_groupbuy_goods"]', function() {
        var goods_commonid = $(this).attr('data-goods-commonid');
        $.get('<?php echo urlShop('store_groupbuy', 'groupbuy_goods_info');?>', {goods_commonid: goods_commonid}, function(data) {
            if(data.result) {
            	var ids = $('[nc_type=0]');
                for(var i=0,j=ids.length; i<j; i++) {
                    if(ids[i].value == data.goods_id+'|0') {
                        return;
                    }
                }
                var html = '<tr class="bd-line">'+
                    '<td><input nc_type="0" id="goods_id" name="goods_id" value="'+data.goods_id+'|0" type="hidden"><input id="goods_name" name="goods_name" value="'+data.goods_name+'" type="hidden"></td>'+
                    '<td><div class="pic-thumb"><img src="'+data.goods_image+'"/></div></td>'+
                    '<td class="tl">'+data.goods_name+'</td>'+
                    '<td><input id="gift_id" name="gift_id" value=""></td>'+
                    '<td><input id="short_name" name="short_name" value=""></td>'+
                    '<td><input id="pro_num" name="pro_num" value=""></td>'+
                    '<td class="nscs-table-handle">'+
                    '<span><a class="del_good_activity" href="javascript:void(0);" class="btn-red"><i class="icon-trash"></i><p><?php echo $lang['nc_del'];?></p></a></span></td>'+
                    '</tr>';

                $("#goods_table").append(html);
                $('.del_good_activity').on('click', function() {
                    $(this).parent().parent().parent().remove();
                });
            } else {
                showError(data.message);
            }
        }, 'json');
    });
    
    //选择优惠券
    $('#div_voucher_search_result').on('click', '[nctype="btn_add_voucher"]', function() {
        var voucher_id = $(this).attr('data-voucher-id');
        $.get('<?php echo urlShop('lottery_activity', 'voucher_info');?>', {voucher_id: voucher_id}, function(data) {
            if(data.result) {
            	var ids = $('[nc_type=1]');
                for(var i=0,j=ids.length; i<j; i++) {
                    if(ids[i].value == data.voucher_t_id+'|1') {
                        return;
                    }
                }
                var html = '<tr class="bd-line">'+
                    '<td><input nc_type="1" id="goods_id" name="goods_id" value="'+data.voucher_t_id+'|1" type="hidden"><input id="goods_name" name="goods_name" value="'+data.voucher_t_title+'" type="hidden"></td>'+
                    '<td><div class="pic-thumb"><img src="'+data.voucher_t_customimg+'"/></div></td>'+
                    '<td class="tl">'+data.voucher_t_title+'</td>'+
                    '<td><input id="gift_id" name="gift_id" value=""></td>'+
                    '<td><input id="short_name" name="short_name" value=""></td>'+
                    '<td><input id="pro_num" name="pro_num" value=""></td>'+
                    '<td class="nscs-table-handle">'+
                    '<span><a class="del_good_activity" href="javascript:void(0);" class="btn-red"><i class="icon-trash"></i><p><?php echo $lang['nc_del'];?></p></a></span></td>'+
                    '</tr>';

                $("#goods_table").append(html);
                $('.del_good_activity').on('click', function() {
                    $(this).parent().parent().parent().remove();
                });
            } else {
                showError(data.message);
            }
        }, 'json');
    });

    $('.del_good_activity').on('click', function() {
        $(this).parent().parent().parent().remove();
    });

    $(".submit").on('click', function(e) {
    	var ids = $('[id=goods_id]'),names = $('[id=short_name]'),nums=$('[id=pro_num]'),gift_id=$('[id=gift_id]');
    	var goods_ids = '',short_names='',pro_nums='',gift_ids='';
        for(var i=0,j=ids.length; i<j; i++) {
        	goods_ids += ids[i].value + ",";
        }
        if(goods_ids != '') {
            goods_ids = goods_ids.substring(0, goods_ids.length-1);
        }
        $("#goods_ids").val(goods_ids);
        
        for(var i=0,j=names.length; i<j; i++) {
        	short_names += names[i].value + ",";
        }
        if(short_names != '') {
        	short_names = short_names.substring(0, short_names.length-1);
        }
        $("#short_names").val(short_names);
        
        for(var i=0,j=nums.length; i<j; i++) {
        	pro_nums += nums[i].value + ",";
        }
        if(pro_nums != '') {
        	pro_nums = pro_nums.substring(0, pro_nums.length-1);
        }
        $("#pro_nums").val(pro_nums);
        
        for(var i=0,j=gift_id.length; i<j; i++) {
        	gift_ids += gift_id[i].value + ",";
        }
        if(gift_ids != '') {
        	gift_ids = gift_ids.substring(0, gift_ids.length-1);
        }
        $("#gift_ids").val(gift_ids);
        
        $("#add_form").submit();
    });

    //页面输入内容验证
    $("#add_form").validate({
        errorPlacement: function(error, element){
            var error_td = element.parent('dd').children('span');
            error_td.append(error);
        },
        onfocusout: false,
    	submitHandler:function(form){
    		ajaxpost('add_form', '', '', 'onerror');
    	},
        rules : {
        	activity_name: {
                required : true
            },
            goods_id: {
                required : true
            }
        },
        messages : {
        	activity_name: {
                required : '<i class="icon-exclamation-sign"></i>活动名称不能为空!'
            },
            activity_intro: {
                required : '<i class="icon-exclamation-sign"></i>活动规则不能为空!'
            },
            goods_id: {
                required : '<i class="icon-exclamation-sign"></i>请选择关联商品!'
            }
        }
    });
});
</script>
